<?php
/**
 * Copyright (c) 2009, Jacek Karczmarczyk
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name of the author nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * PHP Version 5
 *
 * @package Larchana_View
 * @version 0.1
 * @author Jacek Karczmarczyk <jacek@karczmarczyk.pl>
 */


/**
 * See the description of Larchana_View_Helper_HtmlSortableTable
 */
class Larchana_View_Helper_HtmlTableRow extends Zend_View_Helper_HtmlElement
{
	protected $_headers;

	/**
	 *
	 * @param array $headers Columns definitions (see Larchana_View_Herlper_HtmlSortableTable)
	 * @param Object $item Item to display
	 * @param array $options Options (selectable boolean, classHelper string|array, noTr boolean)
	 * @return <type>
	 */
	public function htmlTableRow(array $headers, $item, array $options = null)
	{
		$xhtml = '';
		$this->_headers = $headers;
		if (is_null($options)) $options = array();

		// Open the TR tag, add class if required
		//
		if (!isset($options['noTr']) || !$options['noTr'])
		{
			$xhtml .= '<tr';
			if (isset($options['classHelper']))
			{
				$helperName = $options['classHelper'];
				if (is_array($helperName))
				{
					$helperData = isset($helperName['data']) ? $helperName['data'] : null;
					$helperName = $helperName['name'];
				}
				else
				{
					$helperData = null;
				}
				$helper = $this->view->getHelper($helperName);
				$xhtml .= $this->_htmlAttribs(array('class'=>$helper->$helperName($item, $helperData)));
			}
			$xhtml .= '>'.self::EOL;
		}

		// Add checkbox if selectable
		//
		if (isset($options['selectable']) && $options['selectable'])
		{
			if (is_string($options['selectable']))
				$checkboxName = $options['selectable'];
			else
				$checkboxName = get_class($item);
			$xhtml .= '<td>'.$this->view->formCheckbox($checkboxName.'['.$item->id.']').'</td>'.self::EOL;
		}

		// Add cells
		//
		foreach($headers as $name=>$header) $xhtml .= $this->_getCell($name, $item).self::EOL;

		// Close the TR tag
		//
		if (!isset($options['noTr']) || !$options['noTr'])
		{
			$xhtml .= '</tr>'.self::EOL;
		}

		return $xhtml;
	}

	/**
	 * Creates the HTML code of the TD element
	 *
	 * @param string $name Name of the column
	 * @param mixed $item Displayed row values
	 * @return string HTML code of the TD element (including the TD tag)
	 */
	protected function _getCell($name, $item)
	{
		$header = $this->_headers[$name];

		// Start the TD tag with attributes
		//
		$xhtml = '<td';
		if (isset($header['class'])) $xhtml .= $this->_htmlAttribs(array('class'=>$header['class']));
		$xhtml .= '>';

		// Use helper to create the cell content (if applicable)
		//
		if (isset($header['helper']))
		{
			$helperName = $header['helper'];
			$helper = $this->view->getHelper($helperName);
			$content = $helper->$helperName($item[$name], isset($header['data']) ? $header['data'] : null);
		}
		else if (isset($header['rowHelper']))
		{
			$helperName = $header['rowHelper'];
			$helper = $this->view->getHelper($helperName);
			$content = $helper->$helperName($item, isset($header['data']) ? $header['data'] : null);
		}
		else
		{
			$content = $item[$name];
		}

		// Escape the content (if applicable)
		//
		if (!isset($header['escape']) || $header['escape'])
			$content = $this->view->escape($content);

		$xhtml .= $content;

		// Close the TD tag
		//
		$xhtml .= '</td>';
		return $xhtml;
	}
}
